﻿using NMF.Expressions.Linq;
using System;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Windows;

namespace WpfDemo
{
    /// <summary>
    /// Interaktionslogik für MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private ObservableCollection<Person> employees = new ObservableCollection<Person>();

        public MainWindow()
        {
            InitializeComponent();

            EmployeeList.ItemsSource = employees;

            var employeesWithUpdates = employees.WithUpdates();

            // Does not work because INotifyCollectionChanged implementation does not provide an index on remove
            Log.ItemsSource = from employee in employeesWithUpdates
                              where employee.WorkItems > 2 *
                                 (from collegue in employeesWithUpdates
                                  where collegue.Team == employee.Team
                                  select collegue.WorkItems).Average()
                              select employee.Name;
        }

        private void AddClick(object sender, RoutedEventArgs e)
        {
            employees.Add(new Person() { Name = "<Name>", Team = "<Team>" });
        }

        private void RemoveClick(object sender, RoutedEventArgs e)
        {
            var employee = EmployeeList.SelectedItem as Person;
            if (employee != null) employees.Remove(employee);
        }
    }

    class Person : INotifyPropertyChanged
    {
        #region Name

        private string mName;

        public string Name
        {
            get
            {
                return mName;
            }
            set
            {
                if (mName != value)
                {
                    mName = value;
                    OnNameChanged(EventArgs.Empty);
                }
            }
        }

        protected virtual void OnNameChanged(EventArgs e)
        {
            if (NameChanged != null) NameChanged(this, e);
            OnPropertyChanged("Name");
        }

        public event EventHandler NameChanged;

        #endregion

        #region WorkItems

        private int mWorkItems;

        public int WorkItems
        {
            get
            {
                return mWorkItems;
            }
            set
            {
                if (mWorkItems != value)
                {
                    mWorkItems = value;
                    OnWorkItemsChanged(EventArgs.Empty);
                }
            }
        }

        protected virtual void OnWorkItemsChanged(EventArgs e)
        {
            if (WorkItemsChanged != null) WorkItemsChanged(this, e);
            OnPropertyChanged("WorkItems");
        }

        public event EventHandler WorkItemsChanged;

        #endregion

        #region Team

        private string mTeam;

        public string Team
        {
            get
            {
                return mTeam;
            }
            set
            {
                if (mTeam != value)
                {
                    mTeam = value;
                    OnTeamChanged(EventArgs.Empty);
                }
            }
        }

        protected virtual void OnTeamChanged(EventArgs e)
        {
            if (TeamChanged != null) TeamChanged(this, e);
            OnPropertyChanged("Team");
        }

        public event EventHandler TeamChanged;

        #endregion

        protected virtual void OnPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }

        public event PropertyChangedEventHandler PropertyChanged;

    }
}
